﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Nsf.Samples.RetryActivitySample.Client;
using Nsf.Samples.RetryActivitySample.Infrastructure.Actions;
using Nsf.Samples.RetryActivitySample.Infrastructure.Common.Commands;
using Nsf.Samples.RetryActivitySample.ServiceTest.Actions;

namespace Nsf.Samples.RetryActivitySample.ServiceTest
{
    class Program
    {
        private static readonly ActionManager _actionManager;

        static Program()
        {
            _actionManager = CreateActionManager();
        }

        static void Main(string[] args)
        {
            if (args.Length == 0 || args[0] == "/?")
            {
                ShowHelp();
                return;
            }
            IAction action;
            if (!_actionManager.TryCreateAction(args[0], out action))
            {
                ShowHelp();
                return;
            }
            try
            {
                action.Execute(args);
            }
            catch (ValidationException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        static void ShowHelp()
        {
            var assemblyName = Assembly.GetExecutingAssembly().GetName().Name;
            Console.WriteLine();
            Console.WriteLine("This application is intended to demostrate the use of workflow queues.");
            Console.WriteLine();
            Console.WriteLine("Usage:");
            Console.WriteLine("{0} <Action> [help/Set of arguments]", assemblyName);
            Console.WriteLine("Supported actions:");
            var actions = _actionManager.GetRegisteredActions();
            foreach (var action in actions)
            {
                Console.WriteLine("{0}({1}) - {2}", action.Command, action.Name, action.Description);
            }
            Console.WriteLine();
        }

        static ActionManager CreateActionManager()
        {
            var actionManager = new ActionManager();
            actionManager.RegisterAction<CreateWorkflowAction>("Create");
            actionManager.RegisterAction<RetryWorkflowAction>("Retry");
            actionManager.RegisterAction<UpdateSomeValueWorkflowPropertyAction>("UpdateProperty");
            return actionManager;
        }
    }
}
